const bcrypt = require('bcrypt')
const { User, validateUser } = require('../../../model/user')
module.exports = async(req, res) => {
  let { email, nickName, password, introduction, captcha, captchaReg } = req.body
  if (captcha !== captchaReg) {
    res.send({ meta: { status: 400, msg: '验证码错误！' } });
  } else {
    try {
      validateUser({ email, nickName, password, introduction })
    } catch (e) {
      return res.send({ meta: { status: 400, msg: e.message } })
    }
    const salt = await bcrypt.genSalt(10)
    password = await bcrypt.hash(password, salt)
    try {
      await User.create({ email, nickName, password, introduction })
    } catch (e) {
      console.log(e);
      return res.send({ meta: { status: 400, msg: '注册失败！' } })
    }
    return res.send({ meta: { status: 200, msg: '注册成功！' } })
  }
};
